package com.example.pda.common;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedUtils {
    private static final String SP_NAME = "Scan";

    @SuppressLint("StaticFieldLeak")
    private static Context mContext;
    private static SharedPreferences sp;

    public static void init(Context context) {
        mContext = context;
        sp = mContext.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    }

    public static String getIP() {
        return sp.getString("IP", "");
    }

    public static void setIP(String ip) {
        SharedPreferences.Editor ed = sp.edit();
        ed.putString("IP", ip);
        ed.apply();
    }

    public static String getPort() {
        return sp.getString("Port", "");
    }

    public static void setPort(String ip) {
        SharedPreferences.Editor ed = sp.edit();
        ed.putString("Port", ip);
        ed.apply();
    }

    //存储用户信息
    public static void setDownloadFinish( boolean isDownloadFinish) {
        SharedPreferences.Editor ed = sp.edit();
        ed.putBoolean("download_finish", isDownloadFinish);
        ed.apply();
    }

    //获取用户信息
    public static boolean isDownloadFinish() {
        return sp.getBoolean("download_finish", false);
    }

    public static void setStorage(long time) {
        SharedPreferences.Editor ed = sp.edit();
        ed.putLong("storage", time);
        ed.apply();
    }

    public static long getStorage() {
        return sp.getLong("storage", 0L);
    }



    public static void setUserName(String ip) {
        SharedPreferences.Editor ed = sp.edit();
        ed.putString("UserName", ip);
        ed.apply();
    }

    public static void setPassword(String ip) {
        SharedPreferences.Editor ed = sp.edit();
        ed.putString("Password", ip);
        ed.apply();
    }

    public static String getUserName() {
        return sp.getString("UserName", "");
    }

    /**
     * 清除缓存
     */
    public static void clear() {
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.apply();
    }
}
